package personalPage.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.BaseActivity;
import common.Constant;
import https.InterNetController;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;
import mainFragment.activity.CollegeIndex;
import mainFragment.adapter.RMGXLvAdapter;
import mainFragment.bean.HotCollegeBean;
import view.MyPtrRefresh_LoadView;

/**
 * Created by Administrator on 2017/4/6.
 * 别人关注高校
 */

@ContentView(R.layout.remengaoxiao_activity)
public class CollegeList extends BaseActivity implements View.OnClickListener {

    @ViewInject(R.id.common_title)
    private TextView common_title;
    @ViewInject(R.id.common_back)
    private ImageView common_back;
    @ViewInject(R.id.remengaoxiao_listView)
    private ListView remengaoxiao_listView;
    @ViewInject(R.id.rmgx_load_layout)
    private MyPtrRefresh_LoadView myLoadLayout;

    private int page;
    private static final int REFRESH = 0;
    private static final int LOAD = 1;
    private String userid;
    private String from;
    private ArrayList<HotCollegeBean.CollegeListBean> arryList;
    private RMGXLvAdapter rmgxLvAdapter;
    private MyHandler handler;

    private static class MyHandler extends Handler {
        WeakReference<CollegeList> weakReference;

        public MyHandler(CollegeList weakReference) {
            this.weakReference = new WeakReference<>(weakReference);
        }

        @Override
        public void handleMessage(Message msg) {
            CollegeList activity = weakReference.get();
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    Gson gson = new Gson();
                    HotCollegeBean personalPageDetailBean = gson.fromJson(msg.obj.toString(), HotCollegeBean.class);
                    if (personalPageDetailBean != null) {
                        List<HotCollegeBean.CollegeListBean> collegeList1 = personalPageDetailBean.getCollegeList();
                        if (collegeList1 != null) {
                            switch (msg.arg1) {
                                case REFRESH:
                                    activity.arryList.clear();
                                    for (int i = 0; i < collegeList1.size(); i++) {
                                        activity.arryList.add(collegeList1.get(i));
                                    }
                                    activity.rmgxLvAdapter.notifyDataSetChanged();
                                    break;
                                case LOAD:
                                    for (int i = 0; i < collegeList1.size(); i++) {
                                        activity.arryList.add(collegeList1.get(i));
                                    }
                                    activity.rmgxLvAdapter.notifyDataSetChanged();
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                    break;
                case Constant.ERROR_CODE_FINISH:
                    activity.myLoadLayout.OnRefreshComplete();
                    break;
                default:
                    break;
            }

        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        userid = getIntent().getStringExtra("userId");
        from = getIntent().getStringExtra("from");
        handler = new MyHandler(this);
        initView();
        getDataFromInterNet(REFRESH);
    }

    private void initView() {
        page = 1;
        arryList = new ArrayList<>();
        common_back.setOnClickListener(this);
        common_title.setText("关注院校");
        rmgxLvAdapter = new RMGXLvAdapter(CollegeList.this, arryList);
        myLoadLayout.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                page++;
                getDataFromInterNet(LOAD);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return false;
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {

            }
        });
        remengaoxiao_listView.setAdapter(rmgxLvAdapter);
        remengaoxiao_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i = new Intent();
                i.putExtra("id", arryList.get(position).getId());
                i.putExtra("name", arryList.get(position).getSchool_name());
                i.setClass(CollegeList.this, CollegeIndex.class);
                startActivity(i);
            }
        });
    }


    private void getDataFromInterNet(int flag) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("userid", userid);
        hashMap.put("type", "college");
        hashMap.put("from", from);
        hashMap.put("page", String.valueOf(page));
        new InterNetController(this, Constant.PERSONALDETAIL, handler, hashMap, flag);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_back:
                this.finish();
                break;
            default:
                break;
        }
    }
}
